﻿(function () {
    "use strict";

    var Weapon = WinJS.Class.define(
            function () {
            },
            {
                damage: 0,
                modifier: 0.0,

                toRpcArchive: function () {
                    var result = new Microsoft.Internal.GamesTest.Rpc.Server.RpcArchive();
                    result.setNamedDwordValue("damage", this.damage);
                    result.setNamedDoubleValue("modifier", this.modifier);
                    return result;
                },
            },
            {
                fromRpcArchive: function (archive) {
                    var result = new Weapon();
                    result.damage = archive.getNamedDwordValue("damage")
                    result.modifier = archive.getNamedDoubleValue("modifier");
                    return result;
                },
            });

    var Stature = WinJS.Class.define(
            function () {
            },
            {
                height: 0,
                weight: 0,

                toRpcArchive: function () {
                    var result = new Microsoft.Internal.GamesTest.Rpc.Server.RpcArchive();
                    result.setNamedWordValue("height", this.height);
                    result.setNamedWordValue("weight", this.weight);
                    return result;
                },
            },
            {
                fromRpcArchive: function (archive) {
                    var result = new Stature();
                    result.height = archive.getNamedWordValue("height")
                    result.weight = archive.getNamedWordValue("weight");
                    return result;
                },
            });

    var Player = WinJS.Class.define(
            function () {
            },
            {
                health: -1.0,
                name: null,
                spells: null,
                weapons: null,
                stature: new Stature(),

                toRpcArchive: function (result) {
                    result.setNamedFloatValue("health", this.health);
                    result.setNamedStringValue("name", this.name);
                    result.setNamedStringCollection("spells", this.spells);

                    var weapons = new Array(this.weapons.length);

                    for (var i = 0; i < weapons.length; ++i) {
                        weapons[i] = this.weapons[i].toRpcArchive();
                    }

                    result.setNamedArchiveCollection("weapons", weapons);

                    result.setNamedArchiveValue("stature", this.stature.toRpcArchive());

                    return result;
                },
            },
            {
                fromRpcArchive: function (archive) {
                    var result = new Player();
                    result.health = archive.getNamedFloatValue("health");
                    result.name = archive.getNamedStringValue("name");
                    result.spells = archive.getNamedStringCollection("spells");

                    var weapons = archive.getNamedArchiveCollection("weapons");
                    result.weapons = new Array(weapons.length);

                    for (var i = 0; i < weapons.length; ++i) {
                        result.weapons[i] = Weapon.fromRpcArchive(weapons[i]);
                    }


                    result.stature = Stature.fromRpcArchive(archive.getNamedArchiveValue("stature"));

                    return result;
                },
            });

    WinJS.Namespace.define("RPC.CustomTypes", {

        Weapon: Weapon,

        Stature: Stature,

        Player:  Player,
    });
})();